package com.yonder.consumer;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.yonder.pojo.Message;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @ClassName KafkaConsumer
 * @Description TODO
 * @Author zxs
 * @Date 2021/5/9 16:44
 * Version 1.0
 */
@Component
public class KafkaConsumer {

    @Autowired
    private ObjectMapper objectMapper;

    @KafkaListener(topics = {"yonder"})
    public void listen(ConsumerRecord<?, ?> record) throws IOException {
        String value = (String) record.value();
        System.out.println("接收到的消息：" + objectMapper.readValue(value, Message.class).getMsg());
    }
}
